﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;

namespace ECCSystem
{
	public partial class frmMailKeyName : Form
	{
		private BizRule.ClsMailKeyName objBRMC;
		private int intMode = 0;
		public frmMailKeyName()
		{
			InitializeComponent();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (Check() == false)
			{
				return;
			}
			int intFlg = 0;

			if (ChkAppoint.Checked)
			{
				intFlg = 1;
			}

			if (intMode == 0)
			{
				
				objBRMC = new ECCSystem.BizRule.ClsMailKeyName();
				objBRMC.AddMailKeyName(Convert.ToString(cmbMailCategory.SelectedValue),
									txtMailKeyName.Text.ToString().Trim(),
									txtComment.Text.ToString().Trim(), intFlg);
			}
			else
			{
				objBRMC = new ECCSystem.BizRule.ClsMailKeyName();
				objBRMC.UpdateMailKeyName(Convert.ToString(dgList.SelectedRows[0].Cells[0].Value),
										  Convert.ToString(cmbMailCategory.SelectedValue),
										  txtMailKeyName.Text.Trim(),
										  txtComment.Text.Trim(), intFlg);
				intMode = 0;
			}

			this.btnDelete.Enabled = true;
			this.btnUpdata.Enabled = true;
			this.dgList.Enabled = true;
			this.cmbMailCategory.Text = "";
			this.txtMailKeyName.Text = "";
			this.txtComment.Text = "";
			this.BindDataGrid();
		}

		private bool Check()
		{
			if (clsUICheck.NullToString(cmbMailCategory.SelectedValue).Trim() == "")
			{
				MessageBox.Show("カテゴリ選択必須！", "ECC Information");
				return false;
			}

			if (txtMailKeyName.Text.Trim() == "")
			{
				MessageBox.Show("件名入力必須！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtMailKeyName.Text.Trim(), 50) == false)
			{
				MessageBox.Show("件名入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtComment.Text.Trim(), 50) == false)
			{
				MessageBox.Show("備考入力長さは長すぎ！", "ECC Information");
				return false;
			}

			//重复入力

			if (intMode == 1)
			{
				return true;
			}

			for (int i = 0; i < this.dgList.Rows.Count; i++)
			{
				if (txtMailKeyName.Text.ToString().Trim() == dgList.Rows[i].Cells["CategoryContent_Name"].Value.ToString().Trim())
				{
					if (cmbMailCategory.SelectedValue.ToString().Trim() == dgList.Rows[i].Cells["IDM"].Value.ToString().Trim())
					{
						MessageBox.Show("カテゴリと件名は既に存在しています！", "ECC Information");
						return false;
					}
				}
			}

			return true;
		}

		private void frmMailKeyName_Load(object sender, EventArgs e)
		{
			try
			{
				this.BindDataGrid();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "ECC Catch Error");
			}
		}

		private void frmMailKeyName_FormClosed(object sender, FormClosedEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}

		//bind data
		private void BindDataGrid()
		{
			DataSet objDS = new DataSet();
			DataSet objDS1 = new DataSet();
			objBRMC = new ECCSystem.BizRule.ClsMailKeyName();
			
			objDS = (DataSet)objBRMC.GetList();

			ECCSystem.BizRule.ClsMailCategory objBRMC1 = new ECCSystem.BizRule.ClsMailCategory();
			objDS1 = (DataSet)objBRMC1.GetList();
			if (objDS1.Tables.Count > 0)
			{
				DataTable dt = objDS1.Tables[0];
				DataRow dr = dt.NewRow();
				dr[0] = System.DBNull.Value;
				dr[1] = " ";
				dt.Rows.InsertAt(dr, 0);
				cmbMailCategory.DataSource = dt;
				cmbMailCategory.ValueMember = "CategoryScore_KeyID";
				cmbMailCategory.DisplayMember = "Category_Name";
			}

			//Don't show blank row
			objDS.Tables[0].DefaultView.AllowNew = false;
			dgList.DataSource = objDS.Tables[0];

			if (dgList.Rows.Count > 0)
			{
				dgList_CellClick(null, null);
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				cmbMailCategory.SelectedValue = Convert.ToString(dgList.Rows[0].Cells["IDM"].Value);
				txtMailKeyName.Text = Convert.ToString(dgList.Rows[0].Cells["CategoryContent_Name"].Value);
				txtComment.Text = Convert.ToString(dgList.Rows[0].Cells["CategoryContent_Memo"].Value);
				if (dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value != System.DBNull.Value)
				{
					ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value);
				}
				else
				{
					ChkAppoint.Checked = false;
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
			    //wether have selected row,if have not selected row, pop-up prompt
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                else
                {
                    if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
						objBRMC = new ECCSystem.BizRule.ClsMailKeyName();
                        objBRMC.DeleteMailKeyName(Convert.ToString(dgList.SelectedRows[0].Cells[0].Value));
                        this.BindDataGrid();
						this.cmbMailCategory.Text = "";
						this.txtMailKeyName.Text = "";
						this.txtComment.Text = "";
						ChkAppoint.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
		}

		private void btnUpdata_Click(object sender, EventArgs e)
		{
			if (dgList.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}

			//cmbMailCategory.SelectedValue = Convert.ToString(dgList.SelectedRows[0].Cells["IDM"].Value);
			//txtMailKeyName.Text = Convert.ToString(dgList.SelectedRows[0].Cells["CategoryContent_Name"].Value);
			//txtComment.Text = Convert.ToString(dgList.SelectedRows[0].Cells["CategoryContent_Memo"].Value);
			intMode = 1;

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.cmbMailCategory.SelectedValue = System.DBNull.Value;
			this.txtMailKeyName.Text = "";
			this.txtComment.Text = "";
			intMode = 0;

			if (dgList.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
				dgList.Rows[0].Selected = true;
				cmbMailCategory.SelectedValue = Convert.ToString(dgList.CurrentRow.Cells["IDM"].Value);
				txtMailKeyName.Text = Convert.ToString(dgList.CurrentRow.Cells["CategoryContent_Name"].Value);
				txtComment.Text = Convert.ToString(dgList.CurrentRow.Cells["CategoryContent_Memo"].Value);
				if (dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value != System.DBNull.Value)
				{
					ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value);
				}
				else
				{
					ChkAppoint.Checked = false;
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

		}

		private void dgList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			cmbMailCategory.SelectedValue = System.DBNull.Value;
			txtMailKeyName.Text = "";
			txtComment.Text = "";
			if (dgList.SelectedRows.Count <= 0)
			{
				return;
			}

			cmbMailCategory.SelectedValue = Convert.ToString(dgList.SelectedRows[0].Cells["IDM"].Value);
			txtMailKeyName.Text = Convert.ToString(dgList.SelectedRows[0].Cells["CategoryContent_Name"].Value);
			txtComment.Text = Convert.ToString(dgList.SelectedRows[0].Cells["CategoryContent_Memo"].Value);
			if (dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value != System.DBNull.Value)
			{
				ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["CategoryContent_AppointFlg"].Value);
			}
			else
			{
				ChkAppoint.Checked = false;
			}
			
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			cmbMailCategory.SelectedValue = System.DBNull.Value;
			txtMailKeyName.Text = "";
			txtComment.Text = "";

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 0;
		}
	}
}